package com.wdc.deliveryrobot.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wdc.deliveryrobot.R;

import java.util.List;

public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.ViewHolder>{


    private List<String> locations;
    private OnItemClickListener listener;
    private int selectedPosition = -1; // 跟踪当前选中的按钮

    public interface OnItemClickListener {
        void onItemClick(String location);
    }

    public LocationAdapter(List<String> locations, OnItemClickListener listener) {
        this.locations = locations;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String name = locations.get(position);
        holder.button.setText(name);

        holder.button.setBackgroundResource(R.drawable.rounded_background);
        // 如果当前按钮是选中的按钮，背景为蓝色，否则为白色
//        if (position == selectedPosition) {
//            holder.button.setBackgroundColor(Color.BLUE); // 选中按钮的背景
//        } else {
//            holder.button.setBackgroundColor(Color.WHITE); // 其他按钮恢复白色背景
//        }
        holder.button.setOnClickListener(view -> {
//            if (selectedPosition != position) {
//                selectedPosition = position;
//                notifyDataSetChanged(); // 重新绑定视图，以更新背景色
//            }
            if (listener != null) {
                listener.onItemClick(name); // 传递点击的 location
            }
        });
    }

    @Override
    public int getItemCount() {
        return locations.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public Button button;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.buttonPoint);
        }
    }

    public void notifyData(){
        selectedPosition = -1; // 设置为未选中状态
        notifyDataSetChanged();
    }

}
